#include "optionsState.h"

OptionsState* OptionsState::optionsState = 0;

OptionsState::OptionsState(sf::RenderWindow* window, sfge::StateManager* mgr) : State(window, mgr)
{

}

OptionsState::~OptionsState()
{


}

void OptionsState::init()
{
    backButton = new Button("Back", 400, 500);
    clapBox = new CheckBox("I need applause for my self confidence.", 150, 200);
    clapBox->toggled = applause;
    spaceBox = new CheckBox("I'm feeling spacey.", 150, 260);
    spaceBox->toggled = spaceBackground;
    musicBox = new CheckBox("Music in da house!?", 150, 320);
    musicBox->toggled = playMusic;

    background.SetTexture(rm->getTexture("menu.png"));
    cursorSprite.SetTexture(rm->getTexture("cursor.png"));
}

void OptionsState::cleanup()
{
    playMusic = musicBox->toggled;
    applause = clapBox->toggled;
    spaceBackground = spaceBox->toggled;

    std::ofstream file;
    file.open("Data/settings.txt");
    file << musicBox->toggled <<" "<<spaceBox->toggled<<" "<<clapBox->toggled;
    file.close();

    delete backButton;
    delete clapBox;
    delete spaceBox;
    delete musicBox;
}

void OptionsState::checkButtons(float x, float y)
{
    backButton->checkHL(x, y);
    clapBox->checkHL(x, y);
    spaceBox->checkHL(x, y);
    musicBox->checkHL(x, y);
}

void OptionsState::clicked(float x, float y)
{
    if(backButton->isClicked(x, y))
    {
        stateManager->popState();
        return;
    }
    clapBox->isClicked(x, y);
    spaceBox->isClicked(x, y);
    musicBox->isClicked(x, y);
}

void OptionsState::handle()
{
    sf::Event Event;
    while (window->PollEvent(Event))
    {
        switch (Event.Type)
        {
        case sf::Event::Closed:
            window->Close();
            break;
        case sf::Event::MouseMoved:
            checkButtons(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
            break;
        case sf::Event::MouseButtonPressed:
            if(Event.MouseButton.Button == sf::Mouse::Left)
            {
                clicked(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
                return;
            }
            break;
        default:
            break;
        }
    }
}

void OptionsState::update()
{

}

void OptionsState::draw()
{
    cursorSprite.SetPosition(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
    window->Clear(sf::Color(200, 255, 0));
    window->Draw(background);
    backButton->draw(window);
    clapBox->draw(window);
    spaceBox->draw(window);
    musicBox->draw(window);
    window->Draw(cursorSprite);
    window->Display();
}

